//
//  YunSetupVC.swift
//  YunChenCalculator
//
//  Created by Li Dong on 2023/10/16.
//

import UIKit
import SafariServices
import SnapKit

class YunSetupVC: YunBaseVC {
    lazy var headerView = UIImageView(image: UIImage(named: "bg_me_header"))
    lazy var iconImgView = UIImageView(image: UIImage(named: "icon_wangguan"))
    lazy var dateLabel = UILabel()
    lazy var vipImageView = UIImageView(image: UIImage(named: "img_vip_text"))
    lazy var openVipBtn = UIButton()
    lazy var contentView = UIView()
    lazy var serviceView = createItemView(title: NSLocalizedString("服务协议", comment: ""))
    lazy var privacyView = createItemView(title: NSLocalizedString("隐私政策", comment: ""))

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "设置"
        view.backgroundColor = .init(hex: 0xE0FFD1)
        initHeader()
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 15
        contentView.layer.masksToBounds = true
        view.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(headerView.snp.bottom).offset(40)
            make.height.equalTo(130)
        }
        
        contentView.addSubview(serviceView)
        serviceView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(65)
        }
        
        contentView.addSubview(privacyView)
        privacyView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.serviceView.snp.bottom)
        }
        
        serviceView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemViewClick(sender:))))
        privacyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemViewClick(sender:))))
        NotificationCenter.default.addObserver(self, selector: #selector(reloadHeader), name: .IAPValidateSuccessNotification, object: nil)

    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        reloadHeader()
        if AppProducts.shared.isPurchased {
            adHelper.removeBannerView()
        }else{
            if hasLoadAd {
                return
            }
            hasLoadAd = true
            adHelper.loadBanner(currentVC: self, isMainVC: true)
        }
    }
    
    @objc private func reloadHeader() {
        if AppProducts.shared.isPurchased {
            openVipBtn.isHidden = true
            vipImageView.isHidden = false
//            dateLabel.text = String(format: "%@%@", NSLocalizedString("有效期至:", comment: ""),DateFormatter.formatter.string(from: AppProducts.shared.subscriptionExpirationDate ?? Date()))
            dateLabel.text = ""
        }else{
            openVipBtn.isHidden = false
            vipImageView.isHidden = true
            dateLabel.text = ""
        }

    }
    
    private func initHeader() {
        headerView.contentMode = .scaleAspectFill
        headerView.isUserInteractionEnabled = true
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(20)
            make.height.equalTo(headerView.snp.width).multipliedBy(0.44927536)
        }
        
        headerView.addSubview(iconImgView)
        iconImgView.snp.makeConstraints { make in
            make.top.left.equalTo(15)
        }
        
        dateLabel.textColor = .black
        dateLabel.font = UIFont.systemFont(ofSize: 13)
        headerView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.centerY.equalTo(iconImgView).offset(3)
        }
        
        openVipBtn.setImage(UIImage(named: "icon_open_vip"), for: .normal)
        openVipBtn.addTarget(self, action: #selector(openVip), for: .touchUpInside)
        headerView.addSubview(openVipBtn)
        openVipBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(20)
        }
        
        headerView.addSubview(vipImageView)
        vipImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(10)
        }
        
    }

    private func createItemView(title: String) -> UIView {
        let temp = UIView()
        let label = UILabel()
        label.textColor = .black
        label.font = UIFont.Medium(ofSize: 16)
        label.text = title
        temp.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.centerY.equalToSuperview()
        }
        
        let imgView = UIImageView(image: UIImage(named: "icon_setting_arrow"))
        temp.addSubview(imgView)
        imgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-25)
        }
        
        return temp
    }
    
    @objc func itemViewClick(sender: UIGestureRecognizer) {
        guard let temp = sender.view else {
            return
        }
        var url: URL?
        if temp == serviceView {
            url = URL(string: AppConfig.SERVICE_URL)
        }else {
            url = URL(string: AppConfig.PRIVACY_URL)
        }
        if let tempUrl = url {
            present(SFSafariViewController(url: tempUrl), animated: true, completion: nil)
        }
    }

    @objc func openVip() {
        showSubVC()
    }
}
